package P150;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiangMu
 * @version \$Id: P21.java, v 0.1, 2024年07月04日 19:57 LiangMu Exp $$
 */
public class P21 {

    public static String convert(String s, int numRows) {
        if (numRows == 1) return s;
        List<StringBuilder> result = new ArrayList<>();
        for (int i = 0; i <= numRows; i++) {
            result.add(new StringBuilder());
        }
        char[] chars = s.toCharArray();
        int flag = 1, row = 1;
        for (int i = 0; i < chars.length; i++) {
            int i1 = (i + 1) % numRows;
            result.get(row).append(chars[i]);
            row += flag;
            if (row == 1 || row == numRows) {
                flag *= -1;
            }
        }
        return result.stream().map(StringBuilder::toString).collect(Collectors.joining());
    }


    public static void main(String[] args) {
        String s1 = "PAYPALISHIRING";
        System.out.println(convert(s1, 3).equals("PAHNAPLSIIGYIR"));
        System.out.println(convert(s1, 4).equals("PINALSIGYAHRPI"));
    }


}
